package jml.config;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import com.sun.image.codec.jpeg.TruncatedFileException;

import jml.defaultimple.Sublabel;
import jml.defaultimple.util.UString;

public class LabelGroups
{
	//                filter, <----------- label-groups ----------->
	//                                  label ,<---- sublabels ---->
	private Hashtable<String, Hashtable<String,ArrayList<Sublabel>>> byFilter;
	
	public LabelGroups()
	{
		byFilter = new Hashtable<String, Hashtable<String,ArrayList<Sublabel>>>();
	}
	
	public void addUnderLabel(String filterName,String underLabel)
	{
		Hashtable<String, ArrayList<Sublabel>> byLabel = byFilter.get(filterName);
		if(byLabel==null)
		{
			byLabel=new Hashtable<String, ArrayList<Sublabel>>();
			byFilter.put(filterName, byLabel);
		}
		
		ArrayList<Sublabel> group = byLabel.get(underLabel);
		if(group==null)
		{
			group = new ArrayList<Sublabel>();
			byLabel.put(underLabel, group);
		}
	}
	
	public void addSublabel(String filterName,String labGroupName,String sublabel,boolean showAsLabel,boolean truncateName)
	{
		Hashtable<String, ArrayList<Sublabel>> byLabel = byFilter.get(filterName);
		ArrayList<Sublabel> group = byLabel.get(labGroupName);
		Sublabel sublbl = new Sublabel(sublabel,showAsLabel,truncateName);
		String truncatedName = sublabel;
		if( truncateName )
		{
//			int pos = sublabel.indexOf(labGroupName);
//			truncatedName = pos>=0?sublabel.substring(pos):sublabel;
		
			truncatedName = UString.removeLeft(sublabel, labGroupName);
		}
		
		sublbl.setTuncatedName(truncatedName);
		group.add(sublbl);
	}
	
	public String[] getFilters()
	{
		int i=0;
		String[] filters = new String[byFilter.size()];
		for(Enumeration<String> e=byFilter.keys(); e.hasMoreElements();)
		{
			filters[i++]=e.nextElement();
		}
		
		return filters;
	}
	
	public String[] getGroups(String filterName)
	{
		Hashtable<String, ArrayList<Sublabel>> byGroup = byFilter.get(filterName);
		
		if(byGroup==null)
		{
			return null;
		}
		
		String[] groups = new String[byGroup.size()];
		int i=0;
		for(Enumeration<String> e = byGroup.keys(); e.hasMoreElements(); )
		{
			groups[i++] = e.nextElement();
		}
		
		return groups;
	}
	
	public Sublabel getSublabel(String filterName,String sublabel)
	{
		String groups[] = getGroups(filterName);
		
		if( groups==null )
		{
			return null;
		}
		
		for(String g:groups)
		{
			Sublabel x[] = getSublabels(filterName, g);
			for(Sublabel s:x)
			{
				if(s.getName().equals(sublabel))
				{
					return s;
				}
			}
		}
		return null;
	}
	
	public Sublabel[] getSublabels(String filterName,String group)
	{
		Hashtable<String, ArrayList<Sublabel>> x = byFilter.get(filterName);
		
		if( x ==null )
		{
			return null;
		}
		
		ArrayList<Sublabel> alSublabels = byFilter.get(filterName).get(group);
		
		if( alSublabels==null )
		{
			return null;
		}
		
		Sublabel sublabels[] = new Sublabel[alSublabels.size()];
		return alSublabels.toArray(sublabels);
	}
	
	public String[] getUnderLabelsByFilter(String filter)
	{
		ArrayList<String> underlabels = new ArrayList<String>();
		Hashtable<String, ArrayList<Sublabel>> h = byFilter.get(filter);
		
		if( h==null )
		{
			return null;
		}
		
		for(Enumeration<String> e = h.keys(); e.hasMoreElements(); )
		{
			underlabels.add(e.nextElement());
		}
		
		String ret[]=new String[underlabels.size()];
		return underlabels.toArray(ret);
	}
	
	public String getParent(String filter,String sublabel)
	{
		Hashtable<String, ArrayList<Sublabel>> x = byFilter.get(filter);
		
		if( x == null )
		{
			return null;
		}
		
		
		for(Enumeration<String> e=x.keys(); e.hasMoreElements();)
		{
			String group = e.nextElement();
			ArrayList<Sublabel> s = x.get(group);
			for(Sublabel ss:s)
			{
				if( ss.getName().equals(sublabel) )
				{
					return group;
				}
			}
		}
		
		return null;
	}
	
	public void addFilter(String filter)
	{
		Hashtable<String,ArrayList<Sublabel>> x = byFilter.get(filter);
		if( x== null )
		{
			x = new Hashtable<String, ArrayList<Sublabel>>();
			byFilter.put(filter,x);
		}
	}
	
}
